package com.shelpe.services.sinbadgateway.client;

import java.util.Date;
import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbadgateway.entity.jpa.Adgroup;

import lombok.Data;

@FeignClient(name="${feign.sinbadserver.name}", url="${feign.sinbadserver.url}")
//@FeignClient(name="sinbadserver", url="http://localhost:8086")
public interface SemiautoClient {

//	@RequestMapping(method = RequestMethod.POST, path="/sinbadservice/topricestring",  consumes="application/json;charset=UTF-8")
//	List<String> toPriceStrings(@RequestBody List<KeywordsDto> dtos, @RequestParam("device")int device);
//	
//	@RequestMapping(method = RequestMethod.POST, path="/sinbadservice/setprice",  consumes="application/json;charset=UTF-8")
//	List<com.shelpe.services.sinbadgateway.model.Keyword> setPrice(@RequestBody List<KeywordsDto> dtos, @RequestParam("device")int device, 
//			@RequestParam("userId")Long userId, @RequestParam(value = "nick", required = false)String nick );
//
//	@RequestMapping(method = RequestMethod.POST, path="/sinbadservice/setprices",  consumes="application/json;charset=UTF-8")
//	Object setPrices(@RequestBody List<KeywordsDto> dtos, @RequestParam("device")int device, 
//			@RequestParam("userId")Long userId, @RequestParam(value = "nick", required = false)String nick );
//	
	@RequestMapping(method = RequestMethod.GET, path="/sinbadservice/semiautos/{semiautoId}/adgroups/{adgroupId}/keywords",  consumes="application/json;charset=UTF-8")
	KeywordsDto getKeywords(@PathVariable("semiautoId")Long semiautoId, @PathVariable("adgroupId")Long adgroupId, 
			@RequestParam(value = "userId", required = false)Long userId, 
			@RequestParam(value = "keywordIds", required = false)String keywordIds,
			@RequestParam(value = "matched", required=false)boolean matched);

	@RequestMapping(method = RequestMethod.GET, path="/sinbadservice/adgroupids",  consumes="application/json;charset=UTF-8")
	List<Long> getAdgroupIds(@RequestParam("adgroupId") String adgroupId, @RequestParam("campaignId")String campaignId);
	
	@RequestMapping(method = RequestMethod.GET, value = "/sinbadservice/userid", consumes="application/json;charset=UTF-8")
	Long getUserId(@RequestParam("semiautoId") Long semiautoId);
	
	@RequestMapping(method = RequestMethod.POST, value = "/sinbadservice/updateByKeyword", consumes="application/json;charset=UTF-8")
	int update(@RequestBody com.shelpe.services.sinbadgateway.model.Keyword keyword, @RequestParam("userId")Long userId);
	
	@RequestMapping(method = RequestMethod.GET, value = "/sinbadservice/nick")
	String getNick(@RequestParam("userId")Long userId);
	
	@RequestMapping(method = RequestMethod.POST, value = "/sinbadservice/keyword/logs", consumes="application/json;charset=UTF-8")
	boolean keywordLog(@RequestBody List<KeywordsLog> logs);

	@Data
	public static class KeywordsLog{
	    private Long id;
	    private Long keywordId;
	    private Integer adgroupId;
	    private Byte action;
	    private Integer newIntVal;
	    private Date updatedTime;
	    private String keyword;
	    private String note;
	    public static enum Action {
	    	AUTO_NEW_KEYWORD(0),            //自动加词
	    	AUTO_CHANGE_PRICE(1),           //自动改价
	    	AUTO_DEL_KEYWORD(2),            //自动删词
	    	AUTO_CHANGE_MATCHSCOPE(3),      //自动修改匹配方式
	    	SEMIAUTO_EXEC(4),               //思路执行
	    	MANUAL_NEW_KEYWORD(10),         //手动加词
	    	MANUAL_CHANGE_PRICE(11),        //手动改价
	    	MANUAL_DEL_KEYWORD(12),         //手动删词
	    	MANUAL_CHANGE_MATCHSCOPE(13);   //手动修改匹配方式
	    
			private int value;
			private Action(int v) {
				this.value = v;
			}
			public int getValue() {
				return this.value;
			}
		}
	}
	
	@Data
	public static class KeywordsDto{
		@JsonProperty("adgroup_id")
		private long adgroupID;
		private List<Keyword> words;
		private Adgroup adgroup;
	}

	@Data
	public static class Keyword {
		@JsonProperty("keyword_id")
		private long keywordID;
		private String word;
		@JsonProperty("max_price")
		private float maxPrice;
		@JsonProperty("max_mobile_price")
		private float maxMobilePrice;
		@JsonProperty("impressions")
		private int impression;//展现量
		@JsonProperty("matchscope")
		private int matchScope;
		private int click;//点击量
		@JsonProperty("ctr")
		private float clickRatio;//点击率
		private float cpc;//平均点击花费
		private float cpm;
		private float cvr;//点击转化率
		@JsonProperty("avgpos")
		private int productRanking;//宝贝排名
		private int qscore;//质量得分
		@JsonProperty("wireless_qscore")
		private int mobileQscore;//移动质量得分
		private float cost;//总花费
		@JsonProperty("sales")
		private float sale;//总金额
		@JsonProperty("total_pay_count")
		private int payCount;//成交笔数
		private float roi;//投入产出比
		@JsonProperty("fav_count")
		private int favCount;//收藏数
		@JsonProperty("fav_count_rate")
		private float favCountRatio;//收藏率
		@JsonProperty("cart_count")
		private int cartCount;//购物车数
		@JsonProperty("cart_count_rate")
		private float cartCountRatio;//购物车率
		@JsonProperty("is_default_price")
		private boolean asDefaultPrice;
		@JsonProperty("is_default_mobile_price")
		private boolean asDefaultMobilePrice;

		private String action;
		@JsonProperty("new_matchscope")
		private int newMatchScope;
		@JsonProperty("new_price")
		private float price;
		@JsonProperty("new_mobile_price")
		private float mobilePrice;
		private int modifiedStatus;
		private boolean matched;
		private boolean del;
		private boolean executed;//当天是否执行过
		
		public boolean getAsDefaultPrice(){
			return this.asDefaultPrice;
		}
		
		public boolean getAsDefaultMobilePrice(){
			return this.asDefaultMobilePrice;
		}
		
		public void addAction(String action){
			if(this.action == null){
				this.action = action;
			}else{
				this.action = this.action + "," + action;
			}
		}
	}
}
